package com.gistone.commis.mapper;

import com.gistone.commis.entity.Community;
import com.gistone.commis.entity.Depart;
import com.gistone.commis.entity.Role;
import com.gistone.commis.entity.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * Created by wangfan on 2017/5/1.
 */

@Mapper
public interface UserMapper {

    @Select("SELECT * FROM tb_user left join tb_user_type on\n" +
            "            tb_user.user_type_id= tb_user_type.user_type_id\n" +
            "            where tb_user_type.user_type_name  like '%主%' or  tb_user_type.user_type_name like '%长%' or  tb_user_type.user_type_name like '%领%'")
    List<User> findUserLeads();

    @Select("SELECT * FROM tb_user WHERE user_id = #{userId}")
    @Results(value = {
            @Result(property="userId", column = "user_id"),
            @Result(property="userName", column = "user_name"),
            @Result(property="userPwd", column = "user_pwd"),
            @Result(property="userEmail", column = "user_email"),
            @Result(property="userPhone", column = "user_phone"),
            @Result(property="realName", column = "real_name"),

            @Result(property = "community", column = "community_id",
                    one = @One(select = "com.gistone.commis.mapper.UserMapper.findCommunityById"
                            ,fetchType= FetchType.EAGER)),
            @Result(property = "depart", column = "depart_id",
                    one = @One(select = "com.gistone.commis.mapper.UserMapper.findDepartById"
                            ,fetchType= FetchType.EAGER)),
            @Result(property="roles", column="user_id", javaType= List.class,
                    many=@Many(select="com.gistone.commis.mapper.UserMapper.findRoleByUserId"
                            ,fetchType= FetchType.EAGER))

    })
    User  findUserById(int userId);

    @Select("SELECT * FROM tb_user")
    @Results(value = {
            @Result(property="userId", column = "user_id"),
            @Result(property="userName", column = "user_name"),
            @Result(property="userPwd", column = "user_pwd"),
            @Result(property="userEmail", column = "user_email"),
            @Result(property="userPhone", column = "user_phone"),
            @Result(property="realName", column = "real_name"),

            @Result(property = "community", column = "community_id",
                    one = @One(select = "com.gistone.commis.mapper.UserMapper.findCommunityById"
                            ,fetchType= FetchType.EAGER)),
            @Result(property = "depart", column = "depart_id",
                    one = @One(select = "com.gistone.commis.mapper.UserMapper.findDepartById"
                            ,fetchType= FetchType.EAGER)),
            @Result(property="roles", column="user_id", javaType= List.class,
                    many=@Many(select="com.gistone.commis.mapper.UserMapper.findRoleByUserId"
                            ,fetchType= FetchType.EAGER))

    })
    List<User>  findUsers();




    @Select("SELECT * FROM tb_user WHERE user_name = #{userName}")
    @Results(value = {
            @Result(property="userId", column = "user_id"),
            @Result(property="userName", column = "user_name"),
            @Result(property="userPwd", column = "user_pwd"),
            @Result(property="userEmail", column = "user_email"),
            @Result(property="userPhone", column = "user_phone"),
            @Result(property="realName", column = "real_name"),
            @Result(property="departId", column = "depart_id"),
            @Result(property="communityId", column = "community_id"),
            @Result(property="parentId", column = "parent_id"),
            @Result(property="userTypeId", column = "user_type_id"),


            @Result(property = "community", column = "community_id",
                    one = @One(select = "com.gistone.commis.mapper.UserMapper.findCommunityById"
                            ,fetchType= FetchType.EAGER)),
            @Result(property = "depart", column = "depart_id",
                    one = @One(select = "com.gistone.commis.mapper.UserMapper.findDepartById"
                            ,fetchType= FetchType.EAGER)),
            @Result(property="roles", column="user_id", javaType= List.class,
                    many=@Many(select="com.gistone.commis.mapper.UserMapper.findRoleByUserId"
                            ,fetchType= FetchType.EAGER))

    })
    User  findUserByUserName(String userName);


    @Select("SELECT * FROM tb_community WHERE community_Id = #{communityId}")
    Community findCommunityById (int communityId);

    @Select("SELECT * FROM tb_depart WHERE depart_Id = #{departId}")
    Depart findDepartById (int departId);





    @Select("SELECT * FROM tb_user u LEFT JOIN tb_user_role ru on u.user_id= ru.user_id LEFT JOIN tb_role r on ru.role_id=r.role_id")
    @Results(value = {
            @Result(property="userId", column = "userId"),
            @Result(property="userName", column = "userName"),
            @Result(property="userPwd", column = "userPwd"),
            @Result(property="userEmail", column = "userEmail"),
            @Result(property="userPhone", column = "userPhone"),
            @Result(property="realName", column = "realName"),
            @Result(property="roles", column="roleId", javaType= List.class,
                    many=@Many(select="com.gistone.commis.mapper.UserMapper.selectRoles"))
    })
    List<User> getAllUsers();



    @Select("SELECT * FROM tb_user_role " +
            "WHERE role_id = #{roleId}")
    @Results(value={
            @Result(property="roleId", column ="roleId" ),
            @Result(property="roleName", column = "RoleName")
    })
    List<Role> selectRoles(int roleId);

    @Insert("insert into tb_user_role (user_id,role_id) values (#{0},#{1})")
    int insertUserRole(Integer userId,Integer roleId);

    @Select("SELECT * FROM tb_role WHERE role_id in " +
            "(select role_id from tb_user_role where user_id = #{userId})")
    List<Role> findRoleByUserId(int userId);


    int deleteByPrimaryKey(Integer userId);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Integer userId);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);


    List<User> selectSelective(User user);

}
